package pms.common.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;

import pms.common.utils.DateUtil;
import pms.common.utils.PMSLog;
import pms.common.value.TargetValue;
import pms.common.value.ThemeValue;

public class TargetDaoImpl implements TargetDao {
	
	private JdbcTemplate jdbcTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}


	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	public List<TargetValue> getTargetList(int relId) {
		String sql="select * from tbl_target where rel_id=? and del_fg=0 order by target_id ";
		final List<TargetValue> l_result = new ArrayList<TargetValue>();
		jdbcTemplate.query(sql,new Object[]{relId},new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
				TargetValue l_tv = new TargetValue();
				l_tv.setRelId(rs.getInt("REL_ID"));
				l_tv.setTargetId(rs.getInt("TARGET_ID"));
				l_tv.setTargetName(rs.getString("TARGET_NAME"));
				l_tv.setTargetPath(rs.getString("TARGET_PATH"));
				l_tv.setTargetProgress(rs.getInt("TARGET_PROGRESS"));
				l_tv.setTargetUserId(rs.getInt("TARGET_USER_ID"));
				l_tv.setTargetStatus(rs.getInt("TARGET_STATUS"));
				l_tv.setTargetType(rs.getInt("TARGET_TYPE"));
				l_tv.setTargetCost1(rs.getInt("TARGET_COST1"));
				l_tv.setTargetCost2(rs.getInt("TARGET_COST2"));
				l_tv.setTargetStartDt(rs.getString("TARGET_START_DT"));
				l_tv.setTargetEndDt(rs.getString("TARGET_END_DT"));
				l_tv.setTargetRvUserId(rs.getInt("TARGET_RV_USER_ID"));
				l_result.add(l_tv);
			}			
		});
		return l_result;
	}


	public void deleteTarget(int targetId, int userId) {
		PMSLog.start();
		System.out.println("ccc" + targetId);
		String sql="update tbl_target set del_fg=1 where target_id=?";
		Object[] params = new Object[]{
				targetId
		};
		int result = jdbcTemplate.update(sql, params);
		if(result==0) {
			PMSLog.debug("theme delete 0");
		} else {
			PMSLog.debug("theme deleted");
		}
		PMSLog.end();
	}


	public void insertTarget(TargetValue value) {
		PMSLog.start();
		String sql="insert into tbl_target(rel_id,target_name,target_user_id,target_type,target_status,target_cost1,target_cost2,target_start_dt,target_end_dt,target_rv_user_id) values(?,?,?,?,?,?,?,?,?,?)";
		Object[] params = new Object[]{
				value.getRelId(),
				value.getTargetName(),
				value.getTargetUserId(),
				value.getTargetType(),
				value.getTargetStatus(),
				value.getTargetCost1(),
				value.getTargetCost2(),
				value.getTargetStartDt(),
				value.getTargetEndDt(),
				value.getTargetRvUserId()
		};
		int result = jdbcTemplate.update(sql, params);
		if(result==0) {
			PMSLog.debug("target add 0");
		} else {
			PMSLog.debug("target added");
		}
		PMSLog.end();
		
	}


	public void updateTarget(TargetValue value) {
		PMSLog.start();
		String sql="update tbl_target set target_type=?, target_name=?, target_user_id=?, target_status=?, target_start_dt=?,target_end_dt=?,target_cost1=?,target_cost2=?,target_rv_user_id=?  where target_id=?";
		System.out.println(value.getTargetName() + ":" + value.getTargetType());
		Object[] params = new Object[]{
				value.getTargetType(),
				value.getTargetName(),
				value.getTargetUserId(),
				value.getTargetStatus(),
				value.getTargetStartDt(),
				value.getTargetEndDt(),
				value.getTargetCost1(),
				value.getTargetCost2(),				
				value.getTargetRvUserId(),
				value.getTargetId()
		};
		int result = jdbcTemplate.update(sql, params);
		if(result==0) {
			PMSLog.debug("target update 0");
		} else {
			PMSLog.debug("target updated");
		}
		PMSLog.end();
	}


	public void updateTargetStatus(int targetId, int status) {
		PMSLog.start();
		String sql="update tbl_target set target_status=? where target_id=?";
		Object[] params = new Object[]{
				status,
				targetId
		};
		int result = jdbcTemplate.update(sql, params);
		if(result==0) {
			PMSLog.debug("target update 0");
		} else {
			PMSLog.debug("target updated");
		}
		PMSLog.end();
		
	}

}
